Completed
Push — master ( 402192...fc4c1b )
by Andreas
15:35
created

$(document).ready   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
function refresh_opener(url) {
2
    if (url === undefined) {
3
        url = window.parent.location.href;
4
    }
5
    var button = window.parent.$('[data-dialog="dialog"][data-refresh-opener].active');
6
7
    if (button.length > 0) {
8
        if (   button.data('refresh-opener') === false
9
            && button.closest('.ui-tabs').length === 0) {
10
            close();
11
            return;
12
        }
13
        url = window.parent.location.href;
14
    }
15
    window.parent.location.href = url;
16
}
17
18
function close(data) {
19
    var dialog = window.parent.$('#midcom-dialog');
20
    if (dialog.length > 0) {
21
        dialog
22
            .trigger('dialogsaved', [data])
23
            .dialog('close');
24
    }
25
}
26
27
var extra_buttons = [];
28
function add_dialog_button(url, label, options) {
29
    var button = {
30
        text: label,
31
        'data-action': url,
32
        'class': 'dialog-extra-button',
33
        click: function(){}
34
    };
35
    $.each(options, function(key, value) {
36
        button[key] = value;
37
    });
38
    extra_buttons.push(button);
39
}
40
41
function add_post_button(url, label, options) {
42
    var button = {
43
        text: label,
44
        'class': 'dialog-extra-button',
45
        click: function() {
46
            var form = $('<form action="' + url + '" method="post"></form>'),
47
                dialog = window.parent.$('#midcom-dialog');
48
            $.each(options, function(key, value) {
49
                form.append($('<input type="hidden" name="' + key + '">').val(value));
50
            });
51
            form.appendTo('body').submit();
52
            dialog.dialog('option', 'buttons', []);
53
        }
54
    };
55
    extra_buttons.push(button);
56
}
57
58
$(document).ready(function() {
59
    var title = document.title,
60
        buttons = [];
61
62
    if (   typeof window.parent.$ !== "undefined"
63
        && window.parent.$('#midcom-dialog').length > 0 ) {
64
        var dialog = window.parent.$('#midcom-dialog');
65
        dialog.dialog('option', 'title', title);
66
67
        $('body').on('submit', '.midcom-dialog-delete-form', function(e) {
68
            e.preventDefault();
69
            var form = $(this).detach().appendTo(dialog);
70
71
            form.find('input[name="referrer"]')
72
                .val(window.parent.location.href);
73
74
            //somehow, the original submit button breaks when detaching
75
            form.append($('<input type="hidden" name="' + form.find('input[type="submit"]').attr('name') + '" value="x">'))
76
                .submit();
77
        });
78
79
        $(window).on('unload', function() {
80
            dialog.nextAll('.ui-dialog-buttonpane').find('button')
81
                .prop('disabled', true)
82
                .addClass('ui-state-disabled');
83
        });
84
85
        if ($('.midcom-view-toolbar li').length > 0) {
86
            $('.midcom-view-toolbar li').each(function() {
87
                var btn = $(this).find('a'),
88
                    options = {
89
                        click: function() {
90
                            btn.get(0).click();
91
                            btn.addClass('active');
92
                        }
93
                    };
94
95
                add_dialog_button(btn.attr('href'), btn.text(), options);
96
            });
97
        }
98
99
        if ($('.datamanager2 .form_toolbar > *').length > 0) {
100
            $('.datamanager2 .form_toolbar > *').each(function() {
101
                var btn = $(this);
102
                buttons.push({
103
                    text: btn.val() || btn.text(),
104
                    click: function() {
105
                        if (btn.hasClass('cancel')) {
106
                            dialog.dialog('close');
107
                        } else {
108
                            btn.click();
109
                        }
110
                    }
111
                });
112
            });
113
            $('.datamanager2 .form_toolbar').hide();
114
        }
115
        if (extra_buttons.length > 0) {
116
            buttons = extra_buttons.concat(buttons);
117
        }
118
119
        // This doesn't work under certain circumstances when flexbox is used somewhere in the page:
120
        // dialog.dialog('option', 'buttons', buttons);
121
        // @todo: The root of the problem seems to be that jquery can't set the content element
122
        // to a height of 0, so at some point this could be filed as a bug against their repo. Latest
123
        // stable (3.4.1) is affected. For now, we just copy the relevant part from jqueryui's
124
        // _createButtons method..
125
126
        var buttonset = dialog.nextAll('.ui-dialog-buttonpane').find('.ui-dialog-buttonset').empty();
127
128
        $.each(buttons, function (name, props) {
129
            var click, buttonOptions;
130
            props = $.isFunction(props) ? {click: props, text: name} : props;
131
132
            // Default to a non-submitting button
133
            props = $.extend({type: "button"}, props);
134
135
            // Change the context for the click callback to be the main element
136
            click = props.click;
137
            buttonOptions = {
138
                icon: props.icon,
139
                iconPosition: props.iconPosition,
140
                label: props.text
141
            };
142
143
            delete props.click;
144
            delete props.icon;
145
            delete props.iconPosition;
146
            delete props.text;
147
148
            $('<button></button>', props)
149
                .button(buttonOptions)
150
                .appendTo(buttonset)
151
                .on('click', function() {
152
                    click.apply(dialog[0], arguments);
153
                });
154
        });
155
156
        dialog.find('> .fa-spinner').hide();
157
        dialog.find('> iframe').css('visibility', 'visible');
158
    } else {
159
        $('.midcom-view-toolbar').show();
160
    }
161
});
162